package com.zcoj.controller.User.file;

import com.alibaba.fastjson.JSONObject;
import com.zcoj.core.model.ApiResponse;
import com.zcoj.core.model.Constants;
import com.zcoj.service.FileService;
import com.zcoj.util.FileUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.net.UnknownHostException;
import java.util.Objects;

/**
 * 接口
 * @author 剩xia的盛夏
 * @date 2023/02/24 16:16
 */
@RestController
@Api(tags = "接口")
@RequestMapping("/file")
public class FileController   {

    @Value("${server.port}")
    private String port;

    @Value("${zcoj.address}")
    private String address;
    @Autowired
    private FileService fileService;

    @PostMapping("/img")
    public ApiResponse upload(@RequestBody MultipartFile file){
        File saveFile = fileService.saveFile(file, Constants.File.MARKDOWNIMG_FILE_FOLDER.getPath()+"/");

        System.out.println(saveFile.getPath());
        return null;
    }


    @PostMapping("/mdImg")
    public ApiResponse uploadMdImg(@RequestBody MultipartFile file) throws UnknownHostException {
        if(Objects.isNull(file)){
            return ApiResponse.failed("错误文件");
        }else {
            File mdImg = fileService.uploadFileImg(file, Constants.File.MARKDOWNIMG_FILE_FOLDER.getPath());
            if (Objects.isNull(mdImg)){
                return ApiResponse.failed("图片上传失败");
            }else {
//                return ApiResponse.success("图片上传成功", "http://"+InetAddress.getLocalHost().getHostAddress()+":"+port  +Constants.File.MARKDOWNIMG_FILE_MAPPERING.getPath()+mdImg.getName());
                return ApiResponse.success("图片上传成功", "http://"+address+":"+port  +Constants.File.MARKDOWNIMG_FILE_MAPPERING.getPath()+mdImg.getName());
            }
        }
    }

    @PostMapping("/delImg")
    public ApiResponse deleteMdImg(@RequestBody JSONObject param){
        String path = FileUtil.toDiskPath(param.getString("imgUrl"));
        boolean isDel = cn.hutool.core.io.FileUtil.del(new File(path));
        if(isDel){
            return ApiResponse.success("图片删除成功",null);
        }else{
            return ApiResponse.failed("图片删除失败");
        }
    }

    @PostMapping("/groupImg")
    public ApiResponse uploadGroupImg(@RequestBody MultipartFile file) throws UnknownHostException {
        if(Objects.isNull(file)){
            return ApiResponse.failed("错误文件");
        }else {
            File groupImg = fileService.uploadFileImg(file, Constants.File.GROUP_AVATAR_FOLDER.getPath());
            if (Objects.isNull(groupImg)){
                return ApiResponse.failed("图片上传失败");
            }else {
//                return ApiResponse.success("图片上传成功", "http://"+InetAddress.getLocalHost().getHostAddress()+":"+port  +Constants.File.GROUP_AVATAR_MAPPERING.getPath()+groupImg.getName());
                return ApiResponse.success("图片上传成功", "http://"+address+":"+port  +Constants.File.GROUP_AVATAR_MAPPERING.getPath()+groupImg.getName());
            }
        }
    }


    @PostMapping("/userImg")
    public ApiResponse uploadUserImg(@RequestBody MultipartFile file) throws UnknownHostException {
        if(Objects.isNull(file)){
            return ApiResponse.failed("错误文件");
        }else {
            File userImg = fileService.uploadFileImg(file, Constants.File.USER_AVATAR_FOLDER.getPath());
            if (Objects.isNull(userImg)){
                return ApiResponse.failed("图片上传失败");
            }else {
//                return ApiResponse.success("图片上传成功", "http://"+InetAddress.getLocalHost().getHostAddress()+":"+port  +Constants.File.USER_AVATAR_MAPPERING.getPath()+userImg.getName());
                return ApiResponse.success("图片上传成功", "http://"+address+":"+port  +Constants.File.USER_AVATAR_MAPPERING.getPath()+userImg.getName());
            }
        }
    }

    @PostMapping("/contestImg")
    public ApiResponse uploadContestImg(@RequestBody MultipartFile file) throws UnknownHostException {
        if(Objects.isNull(file)){
            return ApiResponse.failed("错误文件");
        }else {
            File contentImg = fileService.uploadFileImg(file, Constants.File.CONTEST_AVATAR_FOLDER.getPath());
            if (Objects.isNull(contentImg)){
                return ApiResponse.failed("图片上传失败");
            }else {
//                return ApiResponse.success("图片上传成功", "http://"+InetAddress.getLocalHost().getHostAddress()+":"+port  +Constants.File.CONTEST_AVATAR_MAPPERING.getPath()+contentImg.getName());
                return ApiResponse.success("图片上传成功", "http://"+address+":"+port  +Constants.File.CONTEST_AVATAR_MAPPERING.getPath()+contentImg.getName());
            }
        }
    }

}
